package com.otod.commons.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.otod.base.entity.DataEntity;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.DateUtils;

/**
 * 基础controller
 * @author jqiu
 * @version 2013-3-23
 */
public abstract class BaseController {

    public static final String PAGE_SIZE = "3";
    
    /**
     * 参数转换为Map
     * @param request
     * @return
     */
    public Map<String,Object> getParamMapFromRequest(HttpServletRequest request) {
		Map<String,Object> params = new HashMap<String,Object>();
		Map<String,Object> properties = request.getParameterMap();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		while (entries.hasNext()) {
			String value = "";
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value += values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			if(name.startsWith("q_")){					//查询条件命名规范: 以 q_ 开头
				params.put(name.substring(2), value);
			}else{
				params.put(name, value);
			}
		}
		return params;
	}

    /**
     * 添加类新增用户信息
     * @param bean DataEntity
     */
    protected void setInsertOperInfo(DataEntity bean){
    	SessionUserHolder user = SessionUserUtil.getCurrentUser();
    	bean.setCreateEmp(user.getUserName());
    	bean.setCreateEmpNo(user.getUserNo());
    	bean.setCreateIpaddr(user.getLoginIp());
    	bean.setCreateDt(DateUtils.getCurrentDate());
    	bean.setIsDel(CommonConstant.COMMON_NO);
    }
    
    /**
     * 添加类修改用户信息
     * @param bean DataEntity
     */
    protected void setUpdateOperInfo(Map<String,Object> params){
    	SessionUserHolder user = SessionUserUtil.getCurrentUser();
    	params.put("updateEmp", user.getUserName());
    	params.put("updateEmpNo", user.getUserNo());
    	params.put("updateIpaddr", user.getLoginIp());
    	params.put("updateDt", DateUtils.getCurrentDate());
    }
}
